package com.xpgk.mvc.index;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Record;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.right.Right;
import com.xpgk.mvc.user.User;

public class IndexController extends BaseController {


	private IndexService indexService;
//	private NonarrivalService nonarrivalService;
	/**
	 * 首页
	 */
	public void index() {
		User user = getCUser(); // cookie认证自动登陆处理
		if (null != user) {// 后台
			List<Right> menuList = indexService.menu(user);
			String organids = user.getStr(User.column_organizationids);
			Organization organization = Organization.dao.findById(organids);
			setAttr("menuList", menuList);
			setAttr("user", user);
			setAttr("organ", organization);
			render("/index/index.html");
		} else {
			render("/login/login.html");
		}
	}

	/**
	 * 管理中心
	 */
	public void center() {
		String businessdate = "";
		User user = getCUser(); // cookie认证自动登陆处理
		String organids = user.getStr(User.column_organizationids);
		Organization organization = Organization.dao.findById(organids);
		String levelnum = organization.get("levelnum").toString();
		if("0".equals(levelnum) || "1".equals(levelnum)){
			organids = "f73bfba518ff4048afbdc006e3eda906";
		}
		if("4".equals(levelnum)){
			businessdate = new SimpleDateFormat("yyyy").format(new Date());
			List<Record> mm2 = indexService.getRecord2(organids);
			int[] undone = new int[6];
			for (int i = 0; i < mm2.size(); i++) {
				undone[Integer.parseInt(mm2.get(i).getStr("TYPE"))-1] = mm2.get(i).getNumber("UNDONE").intValue();
			}
			setAttr("businessdate", businessdate);
			setAttr("zcdj", undone[0]);
			setAttr("zydj", undone[1]);
			setAttr("bgdj", undone[2]);
			setAttr("zxdj", undone[3]);
			setAttr("bhhp", undone[4]); 
			setAttr("bhzj", undone[5]); 
			
			List<Record> mm = indexService.getRecord(businessdate, organids ,levelnum);
			int[] arr = new int[12];
			for (int i = 0; i < mm.size(); i++) {
				arr[Integer.parseInt(mm.get(i).getStr("YUE"))-1] = mm.get(i).getNumber("TOTALYW").intValue();
			}
			setAttr("arr", Arrays.toString(arr));
			render("/center/center2.html");
		}else{
			businessdate = new SimpleDateFormat("yyyy-MM").format(new Date());
			List<Record> mm = indexService.getRecord(businessdate, organids ,levelnum);
			int length = mm.size();
			int zcdj = 0;
			int zydj = 0;
			int bgdj = 0;
			int zxdj = 0;
			int bhhp = 0;
			int bhzj = 0;
			
			String[] arr = new String[length];
			int[] arr0 = new int[length];
			int[] arr1 = new int[length];
			int[] arr2 = new int[length];
			int[] arr3 = new int[length];
			int[] arr4 = new int[length];
			int[] arr5 = new int[length];
			for (int i=0;i<length;i++) {
				zcdj += (int)mm.get(i).getBigDecimal("ZCGD").intValue();
				zydj += (int)mm.get(i).getBigDecimal("ZYGD").intValue();
				bgdj += (int)mm.get(i).getBigDecimal("BGGD").intValue();
				zxdj += (int)mm.get(i).getBigDecimal("ZXGD").intValue();
				bhhp += (int)mm.get(i).getBigDecimal("BHPGD").intValue();
				bhzj += (int)mm.get(i).getBigDecimal("BZJGD").intValue();
				arr0[i] = (int)mm.get(i).getBigDecimal("ZCGD").intValue();
				arr1[i] = (int)mm.get(i).getBigDecimal("ZYGD").intValue();
				arr2[i] = (int)mm.get(i).getBigDecimal("BGGD").intValue();
				arr3[i] = (int)mm.get(i).getBigDecimal("ZXGD").intValue();
				arr4[i] = (int)mm.get(i).getBigDecimal("BHPGD").intValue();
				arr5[i] = (int)mm.get(i).getBigDecimal("BZJGD").intValue();
				arr[i] = "'"+mm.get(i).get("NAME")+"'";
			}
			setAttr("zcdj", zcdj);
			setAttr("zydj", zydj);
			setAttr("bgdj", bgdj);
			setAttr("zxdj", zxdj);
			setAttr("bhhp", bhhp); 
			setAttr("bhzj", bhzj); 
			setAttr("arr", Arrays.toString(arr));
			setAttr("arr0", Arrays.toString(arr0));
			setAttr("arr1", Arrays.toString(arr1));
			setAttr("arr2", Arrays.toString(arr2));
			setAttr("arr3", Arrays.toString(arr3));
			setAttr("arr4", Arrays.toString(arr4));
			setAttr("arr5", Arrays.toString(arr5));
			setAttr("businessdate", businessdate);
			render("/center/center.html");
		}
	}

	/**
	 * 销售明细曲线图
	 * 
	 * @throws ParseException
	 */
	public void salesDetail() throws ParseException {
//		Organization curOrgan = Organization.dao.findById(getCUserOrganIds());
//		String startTime = ToolDateTime.getMonthOneDay(new Date());
//		String endTime = getPara("endTime", ToolDateTime.format(new Date(), ToolDateTime.pattern_ymd));
//		List<String> days = ToolDateTime.getDaysByStartAndEnd(startTime, endTime);
//		List<String> temp = new ArrayList<String>();
//		List<String> list = new ArrayList<String>();
//		List<String> dates = new ArrayList<String>();
//		List<String> arr = new ArrayList<String>();
//		int k = 0;
//		for (int i = 0; i < days.size(); i++) {
//			temp.add(days.get(i).substring(0, 5));
//		}
//		for (int i = 0; i < temp.size(); i++) {
//			if (!list.contains(temp.get(i))) {
//				list.add(temp.get(i));
//			}
//		}
//		for (int j = 0; j < days.size(); j++) {
//			if (days.get(j).substring(0, 5).equals(list.get(0))) {
//				if (j == 0) {
//					dates.add(days.get(j));
//				} else {
//					dates.add(days.get(j).substring(5, days.get(j).length()));
//				}
//				k = k + 1;
//			}
//		}
//		for (int j = k; j < days.size(); j++) {
//			if (days.get(j).substring(0, 5).equals(list.get(1))) {
//				if (j == k) {
//					arr.add(days.get(j));
//				} else {
//					arr.add(days.get(j).substring(5, days.get(j).length()));
//				}
//			}
//		}
//		dates.addAll(arr);
//		if (null != curOrgan && days.size() > 0) {
//			List<Double> weightSales = indexService.getDelWeightSalesDetail(curOrgan, days);
//			List<Double> productSales = indexService.getProductSalesDetail(curOrgan, days);
//			setAttr("weightSales", weightSales);
//			setAttr("productSales", productSales);
//		}
//		setAttr("dates", dates);
//		setAttr("startTime", startTime);
//		setAttr("endTime", endTime);
		render("/center/saleTrend.html");
	}

	/**
	 * 公告
	 * 
	 */
	public void announce() {
		render("/center/announce.html");
	}

	/**
	 * 预警//当天
	 * 
	 * @throws ParseException
	 * 
	 */
	public void warninggk() throws ParseException {}

	/**
	 * 疗程占比
	 * 
	 */
	public void treatmentRatio() {

		render("/center/coursePercent.html");
	}

	/**
	 * 门店使用情况
	 * 
	 */
	public void mdqk() {

		/*String sqlTem = "select * from sb_customer_temporary where  ISNULL(phone)";
		String sqlCus = "SELECT * from sb_customer where `name` = ? and dataStatus in ('1','2') and organIds = ?";
		List<Temporary> temporaries = Temporary.dao.find(sqlTem);
		for(int i = 0;i<temporaries.size();i++){
			String name = temporaries.get(i).getStr(Temporary.column_name);
			String organIds = temporaries.get(i).getStr(Temporary.column_organIds);
			Customer customer = Customer.dao.findFirst(sqlCus,name,organIds);
			
			temporaries.get(i).set(Temporary.column_phone, customer.getStr(Customer.column_phone));
			temporaries.get(i).update();
		}*/
		String sql = getSqlMy(IndexSQL.sqlId_mdqk);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date d=cal.getTime();
        String date = sdf.format(d);
        String result = "";
		List<Organization> organizations = Organization.dao.find(sql,date,getCUserOrganIds());
		for(int i = 0;i<organizations.size();i++){
			if(i!=(organizations.size()-1)){
				result = result+organizations.get(i).getStr("mdmc")+",";
			}else{
				result = result+organizations.get(i).getStr("mdmc");
			}
			
		}
		
		setAttr("result", result);
		
		setAttr("sizs", organizations.size());
		render("/center/mdqk.html");
	}


	/**
	 * 产品占比
	 * 
	 */
	public void productRatio() {
		render("/center/productPercent.html");
	}

}
